package jircbot;

import java.io.IOException;
import jircbot.irc.Irc;
import jircbot.irc.IrcController;
import jircbot.utils.ConfigReader;

/**
 * TODO: Sort out error handling
 *
 * @author matt
 */
public class JircSys
{
	private Irc irc;
	private IrcController controller;
	private ConfigReader cfgReader;
	private PluginHandler pluginHandler;

	/**
	 * <p>Costruct method.</p>
	 *
	 * <p>Using the configFilePath String, it will load the configuration file
	 * variables and use them to create the Irc object.</p>
	 *
	 * @param configFilePath
	 */
	public JircSys(String configFilePath)
	{
		try
		{
			cfgReader = new ConfigReader(configFilePath);
		}
		catch (IOException ex)
		{
			System.out.println("IOException while reading the config file: " + ex);
			System.exit(1);
		}
	
		irc = new Irc(cfgReader);
		controller = new IrcController(irc);
		pluginHandler = new PluginHandler(irc);

		irc.addObserver(pluginHandler);
	}

	/**
	 * <p>Construct method</p>
	 *
	 * <p>Secod method to construct the JircSys object, uses the Profile class
	 * instead to contain all the variables</p>
	 * 
	 * @param profile
	 */
	public JircSys(Profile profile)
	{
		irc = new Irc(profile);
		controller = new IrcController(irc);
		pluginHandler = new PluginHandler(irc);

		irc.addObserver(pluginHandler);
	}

	/**
	 * <p>Connects to the irc server and starts the plugins.</p>
	 */
	public void start()
	{		
		irc.connect();
	}

	/**
	 * <p>The controller object to send various commands to the Irc object</p>
	 *
	 * @return the controller object.
	 */
	public IrcController controller()
	{
		return controller;
	}

	/**
	 * <p>Adds a plugin to the Irc object</p>
	 * 
	 * @param plugin the plugin to be added
	 */
	public void addPlugin(Plugin plugin)
	{
		pluginHandler.addPlugin(plugin);
	}
}